//#define LINUX

#include <cstdio>
#include <iostream>
#include <cstdlib>

#ifdef LINUX
//#error LINUX
#include <pthread.h>
#include "lServer.h"
//#include "BDD.h"
#include "Socket_Client.h"
#include "Socket_Serveur.h"

#define NB_THREAD 1
#define NUM_PORT_DEBUT 16000

#else
//#error WINDOWS
#include "wServer.h" 
#include "BDD.h"
#include "InfoClient.h"

#endif

using namespace std;

#ifdef LINUX
void *creerThreads(void *voidPortEcoute)
{
  int port = (int) voidPortEcoute;
  cout << "port d'ecoute: " << port << endl;
  Lserver s;
  s.demarrer((unsigned short int) port);

  return NULL;
}

void *creerThreadsimulation(void *arg)
{
  //int arg_simul = (int) arg;
  
  FILE *fichier;
  char* messageRecu = NULL;
  //         char* messageRecu=new char[300];
  int port; 
  char adresse[20];
  messageRecu=new char[300];
  fichier= fopen("./config.txt","r"); 
  if(fichier)
    {
      fscanf(fichier,"%s\n",adresse);
      fscanf(fichier,"%d\n",&port);
    } 
  fclose(fichier);
  cout<< "adresse serv :"<<adresse<<endl;
  cout<< "port serv :"<<port<<endl;
	
  Socket_Client client;

  Socket_Serveur socketServeur(adresse,port,1);
  client = socketServeur.Accepte ();




  //reception de demande d'envoie de plans de vols
  messageRecu = client.receiveFlux();
  cout<<"Message recu par le serveur: "<<messageRecu<<endl;
  delete messageRecu;

  // while(1)
  // 	{
		
  
    // 			messageRecu = socketClient.receiveFlux();
                      
    //                         cout<<"Message recu par le serveur: "<<messageRecu<<endl;

    string s;

    //******************************************************
    //******************************************************
    //******************************************************
    s = "<niveauDeVol>350</niveauDeVol>";  
    //******************************************************
    //******************************************************
    //******************************************************


    cout << "s.c_str() vaut : " << s.c_str() << endl;
    client.sendFlux((char*)s.c_str());

    client.disconect();

    // string t;
    // t="kqsjdkqshdbcndsjdhsdksdfsd*******************************sdqsdqsd**********************************qdqsdqsdqs*********************";
    // client.sendFlux((char*)t.c_str());

    // }
    return NULL;
}

int main(int argc, char** argv)
{
  // cout<<"testtttttttttttttttt"<<endl;
    
  /*  BDD test;
    int z=13;
   // test.GetNiveauDeVol(z);

    cout<<"resultat de la requete :" <<test.GetNiveauDeVol(218)<<endl ;
    
    test.SetNiveauDeVol(z);
    cout<<" insertion effectu� "<<endl;
    
*/

  pthread_t tid_simul;
  int code_simul;
  int arg_simul = 0;  

  cout << "creation du thread pour comminucation avec simulation de la circulation" << endl;
  code_simul = pthread_create(&tid_simul, NULL, creerThreadsimulation, (void *) arg_simul);


  pthread_t tid[NB_THREAD];
  int code;
  int i;

    for (i=0; i< NB_THREAD; i++)
    {
      int port = NUM_PORT_DEBUT +i;
      cout << "creation du thread avec le port: " << port << endl;
      code = pthread_create(&tid[i], NULL, creerThreads, (void *) port);
    }

  for (i=0; i< NB_THREAD; i++){
    code = pthread_join(tid[i], NULL);
  }

  code_simul = pthread_join(tid_simul, NULL);

  return 0;
}

#else

int main(int argc, char *argv[])
{
    
// on instancie un serveur avec comme parametre le port choisi ...
    int port=atoi(argv[1]);
     
    wServer *MyServer = new wServer(port);
//*******************************************************************************************
 
    
   //tester ==> ca fonctionne correctement ...
   // BDD test;
 //  cout<<"resultat de la requete :" <<test.GetNiveauDeVol(328)<<endl ;
    //cout<<"latitude en decimale  "<<test.GetLatitudeAeo("bgaa")<<endl;
   // cout<<"longitude en decimale  "<<test.GetLongitudeAeo("bgaa")<<endl;
    
    ////////// test base pour info client 
    
   // InfoClient clt;
     
 //   clt.SetInfoClientBase("127.200.323.122","31122006 1345",12,"MORT","5555");
  
 
    
      
    
    
   
    
    
//**********************************************************************************************
	if(MyServer->init()!=0){
		cerr << "ne peut initialiser le serveur"<< endl;
		return 1;
	}
  
	if(MyServer->demarrer()!=0){
		cerr << "ne peut d�arrer le serveur"<< endl;
		return 1;
	}
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


#endif
